import 'package:flutter/material.dart';
import 'package:im_module/friend/ChooseContact.dart';
import 'package:im_module/manager/ImChatManager.dart';
import 'package:im_module/model/ImUserInfoBean.dart';
import 'package:im_module/provider/im_provider.dart';
import 'package:im_module/search/SearchFriendWidget.dart';
import 'package:im_module/widget/conversation_widget.dart';
import 'package:provider/provider.dart';

class ConversationWidget extends StatefulWidget {
  final QueryCallback ageCallBack;

  ConversationWidget(this.ageCallBack);

  @override
  _ConversationWidgetState createState() => _ConversationWidgetState();
}

class _ConversationWidgetState extends State<ConversationWidget> {
  @override
  void initState() {
    super.initState();
    print("初始化回话列表");
    ImChatManager().getUnFriendReadCount();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Material(
        child: BodyWidget(widget.ageCallBack),
      ),
    );
  }
}

class BodyWidget extends StatelessWidget {
  final QueryCallback ageCallBack;

  BodyWidget(this.ageCallBack);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Divider(height: .5,),
        ListTile(
          leading: Padding(
            padding: const EdgeInsets.all(2.0),
            child: Icon(
              Icons.add,
              size: 28,
            ),
          ),
          title: Text("发起群聊"),
          onTap: () {
            Navigator.push(context, MaterialPageRoute(builder: (_) {
              return ChooseContactWidget(
                groupId: null,
                groupNumber: [],
              );
            }));
          },
        ),
        /*Divider(),
        ListTile(
          leading: Padding(
            padding: const EdgeInsets.all(2.0),
            child: Icon(
              Icons.search,
              size: 28,
            ),
          ),
          title: Text("添加好友"),
          onTap: () {
            Navigator.push(context, MaterialPageRoute(builder: (_) {
              return SearchFriendWidget(ageCallBack);
            }));
          },
        ),
        Divider(),*/
        /*ListTile(
          leading: Icon(
            Icons.fence,
            size: 28,
          ),
          title: Text("我的好友"),
          onTap: () {
            Navigator.push(
                context, MaterialPageRoute(builder: (c) => FriendListWidget(ageCallBack)));
          },
        ),*/
        /*Divider(),
        Selector(builder: (context, int value, child) {
          return ListTile(
            leading: Badge(
                showBadge: value > 0,
                shape: BadgeShape.circle,
                badgeColor: Colors.red,
                borderRadius: BorderRadius.circular(100),
                alignment: Alignment.topRight,
                toAnimate: false,
                badgeContent: Text('$value',
                    style: TextStyle(color: Colors.white, fontSize: 12)),
                child: Icon(
                  Icons.fence,
                  size: 28,
                )),
            title: Text("好友申请"),
            onTap: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (c) => ApplyFriendListWidget()));
            },
          );
        }, selector: (context, ImProvider provider) {
          return provider.unFriendReadCount;
        }),*/
        Consumer<ImProvider>(
          builder: (context, ImProvider provider, child) {
            return ListView.separated(
              shrinkWrap: true,
              physics: new NeverScrollableScrollPhysics(),
              separatorBuilder: (BuildContext context, int index) {
                return Divider(height: 0.5);
              },
              itemCount: provider.getConversation().length,
              itemBuilder: (context, index) {
                return Container(
                    color: Colors.white,
                    child:
                        ConversationItem(provider.getConversation()[index]!));
              },
            );
          },
        ),
      ],
    );
  }
}

void callBack(context, phone) {
  ImUserInfoBean data = ImUserInfoBean("${phone}", "添加了${phone}为好友",
      "https://sf1-ttcdn-tos.pstatp.com/img/user-avatar/55024cc237dfc23b1c88540e18f707e5~300x300.image");
  Provider.of<ImProvider>(context, listen: false).setQueryUserInfoBean(data);
}
